package com.studentmanagement.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.studentmanagement.util.DBUtil;

public abstract class BaseDAO<T> {
    
    // 通用的查询方法，返回单个对象
    protected T queryForObject(String sql, RowMapper<T> rowMapper, Object... params) throws SQLException {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        T object = null;
        
        try {
            conn = DBUtil.getConnection();
            ps = conn.prepareStatement(sql);
            
            for (int i = 0; i < params.length; i++) {
                ps.setObject(i + 1, params[i]);
            }
            
            rs = ps.executeQuery();
            
            if (rs.next()) {
                object = rowMapper.mapRow(rs);
            }
        } finally {
            DBUtil.close(conn, ps, rs);
        }
        
        return object;
    }
    
    // 通用的查询方法，返回对象列表
    protected List<T> queryForList(String sql, RowMapper<T> rowMapper, Object... params) throws SQLException {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<T> list = new ArrayList<>();
        
        try {
            conn = DBUtil.getConnection();
            ps = conn.prepareStatement(sql);
            
            for (int i = 0; i < params.length; i++) {
                ps.setObject(i + 1, params[i]);
            }
            
            rs = ps.executeQuery();
            
            while (rs.next()) {
                list.add(rowMapper.mapRow(rs));
            }
        } finally {
            DBUtil.close(conn, ps, rs);
        }
        
        return list;
    }
    
    // 通用的更新方法（插入、更新、删除）
    protected int update(String sql, Object... params) throws SQLException {
        Connection conn = null;
        PreparedStatement ps = null;
        
        try {
            conn = DBUtil.getConnection();
            ps = conn.prepareStatement(sql);
            
            for (int i = 0; i < params.length; i++) {
                ps.setObject(i + 1, params[i]);
            }
            
            return ps.executeUpdate();
        } finally {
            DBUtil.close(conn, ps, null);
        }
    }
    
    // 获取总记录数
    protected int getCount(String sql, Object... params) throws SQLException {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        int count = 0;
        
        try {
            conn = DBUtil.getConnection();
            ps = conn.prepareStatement(sql);
            
            for (int i = 0; i < params.length; i++) {
                ps.setObject(i + 1, params[i]);
            }
            
            rs = ps.executeQuery();
            
            if (rs.next()) {
                count = rs.getInt(1);
            }
        } finally {
            DBUtil.close(conn, ps, rs);
        }
        
        return count;
    }
    
    // 行映射器接口
    protected interface RowMapper<T> {
        T mapRow(ResultSet rs) throws SQLException;
    }
}

